<?php

namespace pc\controllers;
use pc\models\Tags;
use Yii;
use pc\models\News;
use pc\controllers\BaseController;

class XinwenController extends BaseController
{
    /*
     * 新闻列表页面
     */
    public function actionIndex()
    {
        $page = (int)Yii::$app->request->get('page') ?: 1;
        //列表新闻
        $result = News::pageNews($page, 10,['id','image','title','content','desc','create_time','view']);
        $params['news'] = $result['data'];

        $pagination = $result['pagination'];
        $pagination['formatUrl'] = '/news/page-{page}.html';
        $pagination['firstUrl'] = '/news/';

        $params['pageString'] = $this->createPageString($pagination);
        $params['page'] = $page;

        return $this->render('index', $params);
    }

    /*
     * 新闻详情页面 详情分页问题
     */
    public function actionInfo()
    {
        $id = (int)Yii::$app->request->get('id');

        $params['newInfo'] = News::getNewsInfoById($id);

        if (empty($params['newInfo'])) $this->redirect('site/error');

        News::incrementView($id,rand(1,3));

        return $this->render('info', $params);
    }


    /**
     * @desc 初始化行文数据到Redis
     */
    public function actionSetRedis()
    {
        $model = new News();
        $news = $model->setRedis();
        for ($i = 0; $i < 20; $i++) {
            $model->setFine(rand(1150, 1255));
        }
    }

    public function actionSetTag(){
        $tag = new Tags();
        $tag->setRedis();
    }


    /*
     * 处理采集的新闻 新闻采集图片
     */
    public function actionFuckImg(){

//        删除采集的重复数据
//        DELETE FROM gtw_newss WHERE id IN (
//        SELECT id FROM (
//            SELECT MIN(id) id,title,COUNT(*) FROM gtw_newss GROUP BY title HAVING COUNT(*) > 1
//        )   tt )

//            delete from gtw_newss where id in (
//                select id from (
//                select b.id from gtw_news a ,gtw_newss b where a.`title` = b.`title`
//            ) tt )

//        插入数据
//        INSERT INTO gtw_news (title,content,images,create_time)
//        SELECT title,content,images,create_time FROM gtw_newss

//        放入redis准备采集图片
//        SELECT a.id,a.`images`,a.`content`,b.article_id FROM gtw_news a LEFT JOIN gtw_newss b ON a.title = b.title  WHERE a.`images` like '%onvshen%' AND a.`id` > 1150

        $sqlCommand = "SELECT a.id,a.`images`,a.`content`,b.article_id FROM gtw_news a LEFT JOIN gtw_newss b ON a.title = b.title  WHERE  a.`id` > 1276";
        $imgs = Yii::$app->db->createCommand($sqlCommand)->queryAll();

        foreach ($imgs as $img){
           Yii::$app->redis->HSet('articleIdRelation',$img['article_id'],$img['id']);

            $_imgs = json_decode($img['images'],true);
            if(!empty($_imgs)){
                foreach ($_imgs as &$_img){
                    Yii::$app->redis->LPush('ariticleImgs',$_img['img']);
                    $_newImagePath = "http://img.guotuw.com/article/{$img['id']}/".basename($_img['img']);
                    $img['content'] = str_replace($_img['img'],$_newImagePath,$img['content']);
                    $_img['img'] = $_newImagePath;
                }
            }
            $img['content']=preg_replace("/<a[^>]*href=[^>]*>|<\/[^a]*a[^>]*>/i","",$img['content']);
            $model = News::findOne(intval($img['id']));
            if($model){
                $model->images = json_encode($_imgs);
                $model->content = $img['content'];
                $rs = $model->save();
                echo $rs ? 1 : 0;
            }
        }

    }

//    public function actionShit(){
//        for($i = 666;$i< 1244;$i++){
//            $model = News::findOne($i);
//            if($model){
//                $model->content = preg_replace("/<a[^>]*href=[^>]*>|<\/[^a]*a[^>]*>/i","",$model->content);
//                $rs = $model->save();
//                echo $rs ? 1: 0;
//            }
//
//        }
//    }





}
